package com.example.goodpet.activity.Fragment;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.goodpet.R;
import com.example.goodpet.activity.adapter.TipsPagerAdapter;
import com.example.goodpet.base.BaseFragment;
import com.example.goodpet.home.fragment.HomeFragment;
import com.example.goodpet.home.homeRecommend.bean.Tips;
import com.example.goodpet.service.HomeService;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.UIUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class PetTipsFragment extends BaseFragment {
    private static final String TAG = HomeFragment.class.getSimpleName();

    private ViewPager2 viewpager;
    private TipsPagerAdapter adapter;
    private Retrofit retrofit;
    private TabLayout tabLayout;

    private TabLayoutMediator mediator;

    private Tips.ResultDTO result;

    private int MAIN=1;
    private int HEALTH=2;
    private int CAT=3;
    private int DOG=4;
    private int OTHER=5;
    /**
     * 初始化主页面Fragment的UI
     */
    @Override
    public View initView() {

        View view = View.inflate(mContext, R.layout.fragment_pet_tips, null);
        tabLayout=view.findViewById(R.id.tabLayout);
        viewpager=view.findViewById(R.id.vp_tips);

        return view;
    }

    @Override
    public void initData() {
        super.initData();
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.JSON_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        retrofit.create(HomeService.class).getTipsJson().enqueue(new Callback<Tips>() {
            @Override
            public void onResponse(Call<Tips> call, Response<Tips> response) {
                Tips body = response.body();
                if(body.getCode()==200){
                    result = body.getResult();
                    //初始化Home适配器，添加fragment进适配器
                    initAdapter();
                }else{
                    UIUtils.toast("未获取到小贴士数据",false);
                }
            }

            @Override
            public void onFailure(Call<Tips> call, Throwable t) {
                UIUtils.toast("网络通信失败",false);
            }
        });

    }
    /**
     * 初始化适配器，添加fragment进适配器
     */
    private void initAdapter() {
        adapter = new TipsPagerAdapter(this,result);
        adapter.addFragment(new TipsContentFragment(),MAIN);
        adapter.addFragment(new TipsContentFragment(),HEALTH);
        adapter.addFragment(new TipsContentFragment(),CAT);
        adapter.addFragment(new TipsContentFragment(),DOG);
        adapter.addFragment(new TipsContentFragment(),OTHER);
        viewpager.setAdapter(adapter);

//        viewpager.setCurrentItem(0);
        mediator=new TabLayoutMediator(tabLayout, viewpager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(UIUtils.getStringArr(R.array.pet_tips_tab)[position]);
            }
        });
        mediator.attach();

    }
}